import {
	ElMessage
} from 'element-plus';
export function getLocation() {
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(showPosition, showError);
	} else {
		console.log("该浏览器不支持获取地理位置");
	}
}

function showPosition(position) {
	console.log("纬度: " + position.coords.latitude +
		"<br>经度: " + position.coords.longitude +
		"<br>位置精度: " + position.coords.accuracy +
		"<br>海拔: " + position.coords.altitude +
		"<br>位置的海拔精度: " + position.coords.altitudeAccuracy +
		"<br>方向: " + position.coords.heading +
		"<br>速度: " + position.coords.speed);
}

export function showError(error) {
	switch (error.code) {
		case error.PERMISSION_DENIED:
			ElMessage({
				type: 'error',
				message: "用户拒绝对获取地理位置的请求",
				duration: 2000,
			})
			break;
		case error.POSITION_UNAVAILABLE:
			ElMessage({
				type: 'error',
				message: "位置信息是不可用的",
				duration: 2000,
			})
			break;
		case error.TIMEOUT:
			ElMessage({
				type: 'error',
				message: "请求用户地理位置超时",
				duration: 2000,
			})
			break;
		case error.UNKNOWN_ERROR:
			ElMessage({
				type: 'error',
				message: "未知错误",
				duration: 2000,
			})
			break;
	}
}